package com.squid.base.config.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池工厂
 *
 * @author jinhang1
 * @date 2024/08/05
 */
@Component
public class ThreadPoolFactory {
    private static final String POOL_NAME = "customPool";
    // 等待队列长度
    private static final int BLOCKING_QUEUE_LENGTH = 10000;
    // 闲置线程存活时间
    private static final int KEEP_ALIVE_TIME = 60 * 1000;
    //核线程数
    private static final int CORE_SIZE = 4;

    @Bean(value = "slowQuerySqlNotifyExecutor")
    public ThreadPoolExecutor slowQuerySqlNotifyThreadPool() {
        return new ThreadPoolExecutor(CORE_SIZE,
                CORE_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(10000),
                new ThreadFactoryBuilder().setNameFormat(POOL_NAME + "_slow_query_sql_notify_" + "-%d").build(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
